package app2;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;
/**
 * 持久层
 * @author Alex
 *
 */
public class BookDao3 {
	/**
	 * 动态查询书籍
	 */
	public List<Book> dynfindAll(Integer id, String name, Double sal) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 Map<String, Object>map=new LinkedHashMap<String, Object>();
			 map.put("pid", id);
			 map.put("pname", name);
			 //模糊查询
			// map.put("pname", name);//BookMapper.xml中等于号改成like语句
			 map.put("psal", sal);
			 //读取BookMapper.xml映射文件的SQL语句
			 return sqlSession.selectList(Book.class.getName()+".dynfindAll",map);		 
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	
	/**
	 * 动态更新书籍
	 */
	public void dynUpdate(Integer id, String name, Double sal) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 Map<String, Object>map=new LinkedHashMap<String, Object>();
			 map.put("pid", id);
			 map.put("pname", name);
			 map.put("psal", sal);
			 //读取BookMapper.xml映射文件的SQL语句
		    sqlSession.selectList(Book.class.getName()+".dynUpdate",map);
		    sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	
	/**
	 * 动态删除书籍
	 */
	public void dynDelete(int ...ids) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //读取BookMapper.xml映射文件的SQL语句
		    sqlSession.selectList(Book.class.getName()+".dynDelete",ids);
		    sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	
	/**
	 * 动态插入书籍
	 */
	public void dynInsert(Book book) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //读取BookMapper.xml映射文件的SQL语句
		    sqlSession.selectList(Book.class.getName()+".dynInsert",book);
		    sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	
	/**
	 * 测试
	 */
	public static void main(String[] args) throws Exception{
		//创建Test对象
		//BookDao3 dao=new BookDao3();
		
		//动态查询
		/*List<Book> bookList1=dao.dynfindAll(null,null,null);
		for (Book book : bookList1) {
			System.out.println(book.getId()+":"+book.getName()+":"+book.getSal());
		}*/
		//模糊查询
		/*List<Book> bookList2=dao.dynfindAll(null,"学",null);
		for (Book book : bookList2) {
			System.out.println(book.getId()+":"+book.getName()+":"+book.getSal());
		}*/
		//更新书籍信息
		//dao.dynUpdate(3,null,10D);
		//dao.dynUpdate(3,"English",null);
		//dao.dynUpdate(3,"英语",20D);
		//删除书籍
		//dao.dynDelete(1, 3, 5);
		//插入书籍
		/*dao.dynInsert(new Book(1,"语文",20));
		dao.dynInsert(new Book(3,"英语",20));
		dao.dynInsert(new Book(4,"物理",15));
		dao.dynInsert(new Book(5,"化学",15));*/
	}
}
